/* 
Copyright DAUPHIN Maxime 2005

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
http://www.gnu.org/licenses/gpl.txt
*/

#include "Gui.h"
#include "Bitmap.h"
#include "Dc.h"

namespace Gui {

Bitmap::Bitmap(void)
{
	m_bCreated = false;
}

Bitmap::~Bitmap(void)
{
	if ( m_bCreated )
		::DeleteObject( m_hBitmap );
}

Bitmap::Bitmap(const Bitmap& bmp)
{
	m_iColorDataType = 0;
	memcpy( &m_bitmap, &bmp.m_bitmap, sizeof(BITMAP) );

	create( bmp.m_hBitmap );

}

void Bitmap::create(HBITMAP hBitmap)
{
	assert(hBitmap);
	m_bCreated = true;
	m_hBitmap = hBitmap;
	::GetObject( m_hBitmap, sizeof(BITMAP), &m_bitmap );

	m_iColorDataType = 0;
	HDC hDC = ::GetDC(NULL);
	memset( &m_bitmapInfo, 0x00, sizeof(BITMAPINFO) );
	BITMAPINFOHEADER &bih = m_bitmapInfo.bmiHeader;
    bih.biSize          = sizeof(BITMAPINFOHEADER);
    bih.biWidth         = m_bitmap.bmWidth;
    bih.biHeight        = m_bitmap.bmHeight;
    bih.biPlanes        = 1;
    bih.biBitCount      = m_bitmap.bmBitsPixel;
	bih.biCompression = BI_RGB;
    bih.biSizeImage     = bytesPerLine(m_bitmap.bmWidth, m_bitmap.bmBitsPixel) * m_bitmap.bmHeight;
    bih.biXPelsPerMeter = 0;
    bih.biYPelsPerMeter = 0;
    bih.biClrUsed       = 0;
    bih.biClrImportant  = 0;

    m_hBitmap = CreateDIBSection( hDC, 
                                 (const BITMAPINFO*) &m_bitmapInfo,
                                 m_iColorDataType,
                                 &m_ppvBits, 
                                 NULL, 0);
	assert(m_hBitmap);

	HDC memDC = ::CreateCompatibleDC(hDC);
	HDC CopyDC = ::CreateCompatibleDC(hDC);

	HBITMAP hOldMemBitmap  = (HBITMAP) ::SelectObject( memDC,  hBitmap);
	HBITMAP hOldCopyBitmap = (HBITMAP) ::SelectObject( CopyDC, m_hBitmap);

	::BitBlt( CopyDC, 0,0, m_bitmap.bmWidth, m_bitmap.bmHeight, memDC, 0,0, SRCCOPY);

	::SelectObject(memDC, hOldMemBitmap);
	::SelectObject(CopyDC, hOldCopyBitmap);

	::DeleteDC( memDC );
	::DeleteDC( CopyDC );
	::DeleteDC( hDC );
}

void Bitmap::draw(Dc &dc, const Rect &rect )
{
	assert(m_bCreated);
	Dc *pDc = dc.createCompatibleDc();
	HBITMAP hbmOld = (HBITMAP)pDc->selectObject( m_hBitmap );
	//dc.bitBlt( rect.t.x, rect.t.y, rect.d.x, rect.d.y, pDc, 0, 0, SRCCOPY );
	dc.stretchBlt( rect, getRect(), pDc, SRCCOPY );
	pDc->selectObject( hbmOld );
	delete pDc;
}

Rect Bitmap::getRect(void)
{
	assert(m_bCreated);
	return Rect( 0, 0, m_bitmap.bmWidth, m_bitmap.bmHeight );
}

HBITMAP Bitmap::getSafeHandle(void)
{
	int size = m_bitmapInfo.bmiHeader.biSizeImage;
	//m_bitmap.bmBits = (LPVOID)new char[size];
	m_bitmap.bmBits = ::GlobalAlloc( GHND | GMEM_SHARE, size );
	HBITMAP hBitmap = ::CreateBitmapIndirect(&m_bitmap);

	HDC hDC = ::GetDC(NULL);
	HDC memDC = ::CreateCompatibleDC(hDC);
	HDC CopyDC = ::CreateCompatibleDC(hDC);

	HBITMAP hOldMemBitmap  = (HBITMAP) ::SelectObject( memDC,  m_hBitmap);
	HBITMAP hOldCopyBitmap = (HBITMAP) ::SelectObject( CopyDC, hBitmap );

	::BitBlt( CopyDC, 0,0, m_bitmap.bmWidth, m_bitmap.bmHeight, memDC, 0,0, SRCCOPY);

	::SelectObject(memDC, hOldMemBitmap);
	::SelectObject(CopyDC, hOldCopyBitmap);

	::DeleteDC( memDC );
	::DeleteDC( CopyDC );
	::DeleteDC( hDC );

	return hBitmap;
}

}; //namespace Gui 
